package com.movecar.adapter;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import com.movecar.model.Pictures;
import com.movecar.utils.AppConfig;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

public class GridImageAdapter extends BaseAdapter {

	private Context mContext;
	private ArrayList<Pictures> dataList;
	private DisplayMetrics dm;
	ImageLoader imageLoader = null;

	public GridImageAdapter(Context c, ArrayList<Pictures> dataList) {

		mContext = c;
		this.dataList = dataList;
		dm = new DisplayMetrics();
		((Activity) mContext).getWindowManager().getDefaultDisplay().getMetrics(dm);
		imageLoader = ImageLoader.getInstance();
	}

	@Override
	public int getCount() {
		return dataList.size();
	}

	@Override
	public Object getItem(int position) {
		return dataList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ImageView imageView;
		if (convertView == null) {
			imageView = new ImageView(mContext);
			imageView.setLayoutParams(new GridView.LayoutParams(150, 150));

			// 设置显示比例类型

			imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);

			imageView.setAdjustViewBounds(true);
		} else
			imageView = (ImageView) convertView;
		// imageView.setImageResource(R.drawable.default_user_photo);
		if (null != dataList.get(position).getUrl()) {
			imageLoader.init(ImageLoaderConfiguration.createDefault(mContext));
			imageLoader.displayImage(AppConfig.HOST + dataList.get(position).getUrl(), imageView);

		}

		return imageView;
	}

	public int dipToPx(int dip) {
		return (int) (dip * dm.density + 0.5f);
	}

}
